package com.liming.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.liming.model.User;
import com.liming.utils.DataSourceUtils;
import com.liming.utils.Md5Utils;

public class UserInfodao {
	QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
	//拿到用户的信息
	public User getInfo(String username) {
		List<User> a=null;
		String sql="select * from user where username=?";
		try {
			a=qr.query(sql, new BeanListHandler<>(User.class),username);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return a.get(0);
	}
	
	public int  changeUserInfo(User user) {
		String sql="update user set realname=?,usersex=?,userphone=?,useremail=? where username=?";
		int flag=0;
		
		Object a[]= {user.getRealname(),user.getUsersex(),user.getUserphone(),user.getUseremail(),user.getUsername()};
		try {
			flag=qr.update(sql,a);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}
	/**
	 * 更新密码
	 * @param username 用户名
	 * @param password  老密码
	 * @param npassword 新密码
	 * @return
	 */
	public int changePassword(String username,String password,String npassword) {
		String sql="update user set password=? where username=? and password=?";
		String newpassword=Md5Utils.md5(npassword);
		int flag=0;
		Object a[]= {newpassword,username,password};
		try {
			flag=qr.update(sql, a);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("修改密码dao执行");
		return flag;
	}
}
